package cn.Y_Qiu.user.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.user.domain.LoginInfo;
import cn.Y_Qiu.user.query.LoginInfoQuery;
import cn.Y_Qiu.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {
    @Autowired
    private ILoginInfoService loginInfoService;

    // 添加
    @PutMapping
    public  Ajaxresult addOrUpdate(@RequestBody LoginInfo loginInfo){
        try {
            if (loginInfo.getId() == null) {
                loginInfoService.add(loginInfo);
            } else {
                loginInfoService.update(loginInfo);
            }
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("炸了");
        }
    }
    // 删除
    @DeleteMapping("/{id}")
    public Ajaxresult delById(@PathVariable("id") Long id){
        try {
            loginInfoService.remove(id);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("失败");
        }
    }
    // 查单
    @GetMapping("/{id}")
    public LoginInfo queryById(@PathVariable("id") Long id){
        return loginInfoService.queryById(id);
    }
    // 查全
    @GetMapping
    public List<LoginInfo> queryAll(){
        return loginInfoService.listAll();
    }
    @PostMapping
    public PageList<LoginInfo> queryPage(@RequestBody LoginInfoQuery loginInfoQuery){
        return loginInfoService.queryPage(loginInfoQuery);
    }
    @PatchMapping
    public Ajaxresult patchremove(@RequestBody List<Long> ids){
        //System.out.println("到达controller");
        try {
            loginInfoService.patchremove(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("失败");
        }
    }
}
